package com.zerox.controller;

import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zerox.domain.TableVO;
import com.zerox.pojo.Supplier;
import com.zerox.pojo.SysUser;
import com.zerox.service.SupplierService;
import com.zerox.service.SysUserService;
import com.zerox.service.impl.SupplierServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version V1.11
 * @ClassName:
 * @Description:
 * @Date
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @GetMapping("/supplier")
    private String getList(){
        return "supplier";//supplier页面
    }

    @GetMapping(value = "/list",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String list(Model model, Integer page, Integer limit,String supName) throws IOException {

        List<Supplier> userList = supplierService.list(page, limit,supName);
        int count = supplierService.count(supName);
        TableVO tableVO = new TableVO(0, count, userList, "");

        String json = JSON.toJSONString(tableVO);

        return json;
    }

    @GetMapping("/toSupplierAdd")
    public String toSupplierAdd(){
        return  "supplierAdd";
    }

    @PostMapping("/supplierAdd")
    public String add(Model model, @Valid Supplier supplier, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            //获取所有的错误
            List<FieldError> fieldError =bindingResult.getFieldErrors();
            for (FieldError fieldErrors:fieldError) {
                model.addAttribute(fieldErrors.getField()+"Error",fieldErrors.getDefaultMessage());

            }
            return "supplierAdd";
        }

        int result= supplierService.add(supplier);
        if (result>0) {
            return "redirect:supplier";
        }
        model.addAttribute("errorMsg","新增失败");
        return "supplierAdd";
    }

    @GetMapping("/view/{id}")
    public String view(Model model,@PathVariable Integer id){
        Supplier supplier=supplierService.selectId(id);
        model.addAttribute("supplier",supplier);
        return "supplierViews";
    }

    @GetMapping("/toEdit/{id}")
    public String toEdit(Model model,@PathVariable("id") Integer id){
        Supplier supplier=supplierService.selectId(id);
        model.addAttribute("supplier",supplier);
        return "supplierEdit";
    }

    @PostMapping("/edit")
    public String edit(Supplier supplier){
        int update = supplierService.update(supplier);
        if (update>0) {
            return "redirect:supplier";
        }
        return "redirect:supplier";
    }

    @GetMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable Integer id){
        int result=supplierService.del(id);
        return result>0? "1" : "0";
    }


    @GetMapping("supCodeExists")
    @ResponseBody
    public String supCodeExists(String supCode){
        int i = supplierService.selectBysupCode(supCode);
        return i>0? "1":"0";
    }

    
    @GetMapping("/list2")
    public String list2(Map<String,Object> map, @RequestParam(value = "ids",required = false,defaultValue = "0") Integer id){
        System.out.println(this);
        map.put("id",id);
        return "list";
    }

    @GetMapping("/list1")
    public ModelAndView list1(@RequestParam(value = "ids",required = false,defaultValue = "0") Integer id){
        System.out.println(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("list.jsp");
        modelAndView.addObject("id",id);
        return modelAndView;
    }
}